import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
export const useUserStore = defineStore('user', () => {
    const token = ref<string>('');
    const adminid = ref<string>('');
    const role = ref<number>(1);
    const adminname = ref<string>("");
    const checkedKeys = ref<string[]>([]);
    // 执行这个函数，本地数据库中的token变化，store中token也发生了变化
    const changeToken = (newToken: string) => {
        token.value = newToken;
    }
    const changeAdminId = (newAdminId: string) => {
        adminid.value = newAdminId;
    }
    const changeRole = (newRole:number)=>{
        role.value = newRole
    }
    const  changeCheckedKeys = (newKeys:string[])=>{
       checkedKeys.value = newKeys
    }
    const changeAdminName = (newName:string)=>{
        adminname.value = newName
    }
    // 退出的方法
    const loginout = () => {
        token.value = '';
        adminid.value = ''
    }
    // 判断是否登录的函数
    const isLogin = computed<boolean>(() => token.value ? true : false);
    return {
        token,
        adminid,
        changeAdminId,
        changeToken,
        loginout,
        isLogin,
        role,
        changeRole,
        checkedKeys,
        changeCheckedKeys,
        adminname,
        changeAdminName
    }
}, {
    persist: true,
  })